package source;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 * @author Sabitov Artem
 */
public class mpSync {
	public static boolean bLog;
	// синхронизирует экипажи
	public static HashMap syncCrews(HashMap firstCrews, HashMap secondCrewsArg, boolean bArgLog) {
		HashMap<String, ArrayList<String>> secondCrews = new HashMap();
		secondCrews = (HashMap<String, ArrayList<String>>) secondCrewsArg.clone();
		bLog = bArgLog;
		boolean bEqualFound = false;
		ArrayList<String> listFirst;
		ArrayList<String> listSecond = null;
		Collection allKeyFirst = firstCrews.keySet();
		Collection allKeySecond = secondCrews.keySet();
		ArrayList<String> delFromFirst = new ArrayList<>();
		ArrayList<String> delFromSecond = new ArrayList<>();
		
		for(Iterator i = allKeyFirst.iterator(); i.hasNext();) {
			Object oI = i.next();
			listFirst = (ArrayList<String>) firstCrews.get(oI);
			for(Iterator j = allKeySecond.iterator(); j.hasNext();) {
				Object oJ = j.next();
				listSecond = (ArrayList<String>) firstCrews.get(oJ);
				try {
					if(listFirst.get(2).equals(listSecond.get(2))) {
						delFromSecond.add(oJ.toString());
						//secondCrews.remove(j);
						bEqualFound = true;
					}
				} catch (NullPointerException ex) {}
			}
			if(!bEqualFound && !mpMain.bFirstUpdate) {
				syncLog("remove crew: " + i);
				delFromFirst.add(oI.toString());
				//firstCrews.remove(i);
				bEqualFound = false;
			}
		}
		
		for(Iterator i = delFromFirst.iterator(); i.hasNext();) {
			firstCrews.remove(i.next());
		}
		for(Iterator j = delFromSecond.iterator(); j.hasNext();) {
			secondCrews.remove(j.next());
		}
		
		allKeySecond = secondCrews.keySet();
		if(!secondCrews.isEmpty()) {
			for(Iterator i = allKeySecond.iterator(); i.hasNext();) {
				Object oObj = i.next();
				String sName = oObj.toString();
				firstCrews.put(sName, secondCrews.get(oObj));
				syncLog("add crew: " + sName.trim());
			}
		}
		return firstCrews;
	}
	
	// синхронизирует судна
	public static HashMap syncVessels(HashMap firstVessels, HashMap secondVesselsArg, boolean bArgLog) {
		HashMap<String, ArrayList<String>> secondVessels = new HashMap();
		secondVessels = (HashMap<String, ArrayList<String>>) secondVesselsArg.clone();
		bLog = bArgLog;
		boolean bEqualFound = false;
		ArrayList<String> listFirst;
		ArrayList<String> listSecond;
		Collection allKeyFirst = firstVessels.keySet();
		Collection allKeySecond = secondVessels.keySet();
		ArrayList<String> delFromFirst = new ArrayList<>();
		ArrayList<String> delFromSecond = new ArrayList<>();
		
		for(Iterator i = allKeyFirst.iterator(); i.hasNext();) {
			bEqualFound = false;
			Object oI = i.next();
			listFirst = (ArrayList<String>) firstVessels.get(oI);
			for(Iterator j = allKeySecond.iterator(); j.hasNext() && !bEqualFound;) {
				Object oJ = j.next();
				listSecond = (ArrayList<String>) secondVessels.get(oJ);
				try {
					if(listFirst.get(2).equals(listSecond.get(2))) {
						bEqualFound = true;
						if(equalsPart(listFirst, listSecond)
								&& equalsOrbit(listFirst, listSecond)
								&& equalsState(listFirst, listSecond)) {
							delFromSecond.add(oJ.toString());
							//secondVessels.remove(oJ);
						} else {
							syncLog("update vessel: " + listFirst.get(3).trim());
							delFromFirst.add(oI.toString());
							//firstVessels.remove(oI);
						}
					}
				} catch (NullPointerException ex) {}
			}
			if(!bEqualFound) {// && !mpMain.bFirstUpdate) {
				syncLog("remove vessel: " + listFirst.get(3).trim());
				delFromFirst.add(oI.toString());
				//firstCrews.remove(i);
				bEqualFound = false;
			}
		}
		
		for(Iterator i = delFromFirst.iterator(); i.hasNext();) {
			firstVessels.remove(i.next());
		}
		for(Iterator j = delFromSecond.iterator(); j.hasNext();) {
			secondVessels.remove(j.next());
		}
		
		allKeySecond = secondVessels.keySet();
		if(!secondVessels.isEmpty()) {
			for(Iterator i = allKeySecond.iterator(); i.hasNext();) {
				Object oObj = i.next();
				String sName = oObj.toString();
				firstVessels.put(sName, secondVessels.get(oObj));
				ArrayList<String> listTemp = (ArrayList<String>) secondVessels.get(oObj);
				syncLog("add vessel: " + listTemp.get(3).trim());
			}
		}
		return firstVessels;
	}
	
	private static boolean equalsPart(ArrayList<String> VesselFirst, ArrayList<String> VesselSecond) {
		int iFirstCounter = 0;
		int iSecondCounter = 0;
		for(Iterator i = VesselFirst.iterator(); i.hasNext();) {
			if(i.next().toString().equals(mpPattern.PART)) {
				iFirstCounter++;
			}
		}
		for(Iterator i = VesselSecond.iterator(); i.hasNext();) {
			if(i.next().toString().equals(mpPattern.PART)) {
				iSecondCounter++;
			}
		}
		return iFirstCounter == iSecondCounter;
	}
	
	private static boolean equalsOrbit(ArrayList<String> VesselFirst, ArrayList<String> VesselSecond) {
		ArrayList<String> FirstOrbit = new ArrayList<>();
		ArrayList<String> SecondOrbit = new ArrayList<>();
		Object oObj;
		for(Iterator i = VesselFirst.iterator(); i.hasNext();) {
			oObj = i.next();
			if(oObj.toString().equals(mpPattern.ORBIT)) {
				// 12 строк в секции орбиты
				FirstOrbit.add(oObj.toString());
				for(int j = 0; j < 11; j++) {
					FirstOrbit.add(i.next().toString());
				}
			}
		}
		for(Iterator i = VesselSecond.iterator(); i.hasNext();) {
			oObj = i.next();
			if(oObj.toString().equals(mpPattern.ORBIT)) {
				// 12 строк в секции орбиты
				SecondOrbit.add(oObj.toString());
				for(int j = 0; j < 11; j++) {
					SecondOrbit.add(i.next().toString());
				}
			}
		}
		return FirstOrbit.equals(SecondOrbit);
	}
	
	private static boolean equalsState(ArrayList<String> VesselFirst, ArrayList<String> VesselSecond) {
		ArrayList<String> FirstState = new ArrayList<>();
		ArrayList<String> SecondState = new ArrayList<>();
		Object oObj;
		for(Iterator i = VesselFirst.iterator(); i.hasNext();) {
			oObj = i.next();
			try {
				if(oObj.toString().substring(0, 9).equals("			met = ")) {
					// 10 строк состояния
					FirstState.add(oObj.toString());
					for(int j = 0; j < 9; j++) {
						FirstState.add(i.next().toString());
					}
				}
			} catch (StringIndexOutOfBoundsException ex) {}
		}
		for(Iterator i = VesselSecond.iterator(); i.hasNext();) {
			oObj = i.next();
			try {
				if(oObj.toString().substring(0, 9).equals("			met = ")) {
					// 10 строк состояния
					SecondState.add(oObj.toString());
					for(int j = 0; j < 9; j++) {
						SecondState.add(i.next().toString());
					}
				}
			} catch (StringIndexOutOfBoundsException ex) {}
		}
		return FirstState.equals(SecondState);
	}
	
	private static void syncLog(Object oObj) {
		if(bLog) {
			mpLog.syncLog(oObj);
		}
	}
}
